package com.jxd.project.wzgl.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jxd.project.wzgl.dao.IGoodsDao;
import com.jxd.project.wzgl.dto.DeleteGoodsDTO;
import com.jxd.project.wzgl.dto.GoodsDTO;
import com.jxd.project.wzgl.dto.QueryGoodsDTO;
import com.jxd.project.menu.model.Goods;
import com.jxd.project.menu.model.GoodsLog;
import com.jxd.project.wzgl.service.IGoodsLogService;
import com.jxd.project.wzgl.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.*;

/**
 * @ClassName GoodsServiceImpl
 * @Description Goods Service Implementation
 * @Author 刘成
 * @Date 2025/10/1 20:00
 * @Version 1.0
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<IGoodsDao, Goods> implements IGoodsService {
    @Autowired
    @Qualifier("goodsServiceImpl")
    private IGoodsService goodsService;
    @Autowired
    @Qualifier("goodsLogServiceImpl")
    private IGoodsLogService goodsLogService;
    @Autowired
    private IGoodsDao goodsDao;

    @Override
    @Transactional
    public boolean addGoods(GoodsDTO goodsDTO) {
        Goods goods = goodsDTO.getGoods();
        GoodsLog goodsLog = goodsDTO.getGoodsLog();
        boolean result = goodsDao.insert(goods) > 0;
        goodsLog.setGoodsId(goods.getGoodsId());

        return result && goodsLogService.save(goodsLog);
    }

    @Override
    @Transactional
    public boolean updateGoods(GoodsDTO goodsDTO) {
       Goods goods = goodsDTO.getGoods();
       GoodsLog goodsLog = goodsDTO.getGoodsLog();
       boolean result1 = goodsDao.updateById(goods) > 0;
       boolean result2 = goodsLogService.save(goodsLog);
       return result1 && result2;
    }

    @Override
    @Transactional
    public boolean updateGoodsNum(int goodsId, int changeNum) {
        return goodsDao.updateGoodsNum(goodsId,changeNum);
    }

    @Override
    @Transactional
    public boolean deleteGoods(DeleteGoodsDTO deleteGoodsDTO) {
        List<Integer> goodsIDs = deleteGoodsDTO.getGoodsIDs();
        //查询是否有在借未还的物资
        boolean pass = goodsDao.selectCountWithNotReturn(goodsIDs) == 0;
        if (!pass) {
            return false;
        }
        boolean flag1 = goodsService.removeByIds(goodsIDs);
        LocalDate now = LocalDate.now();
        List<GoodsLog> goodsLogs = new ArrayList<>();
        for (Integer goodsID : goodsIDs) {
            GoodsLog goodsLog = new GoodsLog();
            goodsLog.setGoodsId(goodsID);
            goodsLog.setGoodsChangeNum(0);
            goodsLog.setGoodsChangeType("删除");
            goodsLog.setGoodsChangeTime(now);
            goodsLog.setGoodsChangeReason("物资删除");
            goodsLog.setLoggedEmpNo(deleteGoodsDTO.getLogEmpNo());
            goodsLogs.add(goodsLog);
        }
        boolean flag2 = goodsLogService.saveBatch(goodsLogs);
        return flag1 && flag2;
    }

    @Override
    public Map<String,Object> getGoodsByWarehouseId(QueryGoodsDTO queryGoodsDTO) {
        Map<String,Object> map = new HashMap<>();
        if (queryGoodsDTO.getCurrent() != 0 || queryGoodsDTO.getSize() != 0) {
            Page<Goods> page = new Page<>(queryGoodsDTO.getCurrent(), queryGoodsDTO.getSize());
            List<Goods> list = goodsDao.selectGoodsWithQueryGoodsDTO(page,queryGoodsDTO);
            int total = (int)page.getTotal();
            map.put("total",total);
            map.put("data",list);
            return map;
        }
        List<Goods> list = goodsDao.selectGoodsWithQueryGoodsDTO(queryGoodsDTO);
        map.put("data",list);
        return map;
    }

}